import req from '@/http/http'
import config from '@/config/index'

// zhpq接口
const ZHPQ_SERVER = config.serverUrl()

export const getServer = _ => {
  return ZHPQ_SERVER
}

function buildPath (url, id) {
  return url.replace('{ID}', id)
}

// 设置授权码
const CREATE_AUTHORIZATIONCODE_PATH = ZHPQ_SERVER + '/appUsers/setSpecialPass'
const CHECK_AUTHORIZATIONCODE_PATH = ZHPQ_SERVER + '/appUsers/checkSpecialPass'

export const createAuthorizationCode = data => {
  return req.put(CREATE_AUTHORIZATIONCODE_PATH, data)
}

export const checkAuthorizationCode = data => {
  return req.put(CHECK_AUTHORIZATIONCODE_PATH, data)
}

// 用户登陆
const LOGIN_PATH = ZHPQ_SERVER + '/auth/pc/login'
const LOGOUT_PATH = ZHPQ_SERVER + '/auth/pc/logout'
const CAPTCHA_PATH = ZHPQ_SERVER + '/captcha/base64'
const CAPTCHA_IMG_PATH = ZHPQ_SERVER + '/captcha/img'

// 用户登陆
export const login = data => {
  return req.post(LOGIN_PATH, data)
}

export const logout = param => {
  return req.get(LOGOUT_PATH, param)
}

// 验证码
export const getCaptcha = param => {
  let time = Math.random()
  param = {t: time}
  return req.get(CAPTCHA_PATH, param)
}

export const getCaptchaImg = param => {
  return req.get(CAPTCHA_IMG_PATH, param)
}

// 修改用户密码
export const updateUserPwd = data => {
  let path = ZHPQ_SERVER + '/password/'
  return req.put(path, data)
}

// 角色
const ROLE_U_LIST_PATH = ZHPQ_SERVER + '/roles/u/list'
// 通过层级筛选角色
const ROLE_BYCATEGORY_PATH = ZHPQ_SERVER + '/roles/listByDeptLevel?level='

export const listRoleByCurUser = param => {
  return req.get(ROLE_U_LIST_PATH, param)
}
export const findAllRoleByDeptCategorySel = code => {
  let path = ROLE_BYCATEGORY_PATH + code
  return req.get(path)
}

export const getUserRoles = param => {
  let path = '/userRoles/u/list'
  return req.get(path, param)
}

// 用户
const USER_CREATE_PATH = ZHPQ_SERVER + '/appUsers/'
const USER_PUT_PATH = ZHPQ_SERVER + '/appUsers/{ID}'
const USER_DELETE_PATH = ZHPQ_SERVER + '/appUsers/{ID}'
const USER_GET_PATH = ZHPQ_SERVER + '/appUsers/{ID}'
const USER_FIND_PATH = ZHPQ_SERVER + '/appUsers'
const USER_LIST_PATH = ZHPQ_SERVER + '/appUsers/list'
const USER_SIGNINFO_PATH = ZHPQ_SERVER + '/SignInfo/getUserDetail'

export const createUser = data => {
  return req.post(USER_CREATE_PATH, data)
}

export const getSignInfo = id => {
  let path = USER_SIGNINFO_PATH + '?userId=' + id
  return req.get(path)
}

export const getPicPath = _ => {
  let path = ZHPQ_SERVER + '/image/'
  return path
}

export const updateUser = data => {
  let path = buildPath(USER_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteUser = data => {
  let path = buildPath(USER_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const getUser = id => {
  let path = buildPath(USER_GET_PATH, id)
  return req.get(path)
}

export const findUser = param => {
  return req.get(USER_FIND_PATH, param)
}

export const getUserList = param => {
  return req.get(USER_LIST_PATH, param)
}
export const getUserListDepart = id => {
  let path = '/appUsers/listByGridId?gridId=' + id
  return req.get(path)
}
export const getUserDetail = _ => {
  let path = '/appUsers/u'
  return req.get(path)
}

export const updateUserDetail = data => {
  let path = '/appUsers/u/'
  return req.put(path, data)
}

// 权限
const PERMISSION_USER_TREE_PATH = ZHPQ_SERVER + '/permissions/u/tree'
const PERMISSION_USER_BTN_PATH = ZHPQ_SERVER + '/permissions/u/{ID}/btns'

export const findUserPermissionTree = param => {
  return req.get(PERMISSION_USER_TREE_PATH, param)
}

export const findUserPermissionBtnsByParent = id => {
  let path = buildPath(PERMISSION_USER_BTN_PATH, id)
  return req.get(path, null)
}

// 行政区
const REGION_CREATE_PATH = ZHPQ_SERVER + '/regions/'
const REGION_PUT_PATH = ZHPQ_SERVER + '/regions/{ID}'
const REGION_DELETE_PATH = ZHPQ_SERVER + '/regions/{ID}'
const REGION_LIST_PATH = ZHPQ_SERVER + '/regions/list'
const REGION_TREE_PATH = ZHPQ_SERVER + '/regions/tree'
const REGION_FIND_PATH = ZHPQ_SERVER + '/regions'
const REGION_GET_PATH = ZHPQ_SERVER + '/regions/'

// 行政区
export const listRegion = param => {
  return req.get(REGION_LIST_PATH, param)
}

export const createRegion = data => {
  return req.post(REGION_CREATE_PATH, data)
}

export const updateRegion = data => {
  let path = buildPath(REGION_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteRegion = data => {
  let path = buildPath(REGION_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const getRegionTree = param => {
  return req.get(REGION_TREE_PATH)
}

export const findRegion = param => {
  return req.get(REGION_FIND_PATH, param)
}

export const getRegion = id => {
  let path = REGION_GET_PATH + id
  return req.get(path)
}

// 部门分类
const DEPT_CATE_ALL_PATH = ZHPQ_SERVER + '/deptCategorys/all'

export const listDeptCate = param => {
  return req.get(DEPT_CATE_ALL_PATH, param)
}

// 部门
const DEPT_CREATE_PATH = ZHPQ_SERVER + '/depts/'
const DEPT_PUT_PATH = ZHPQ_SERVER + '/depts/{ID}'
const DEPT_DELETE_PATH = ZHPQ_SERVER + '/depts/{ID}'
const DEPT_FIND_PATH = ZHPQ_SERVER + '/depts'
const DEPT_LIST_PATH = ZHPQ_SERVER + '/depts/list'
const DEPT_TREE_PATH = ZHPQ_SERVER + '/depts/tree'
const DEPT_LEVEL_PATH = ZHPQ_SERVER + '/depts/sameLevel?userId='
const GRID_LEVEL_PATH = ZHPQ_SERVER + '/grids/sameLevel?userId='
const GET_REGION_BY_DEPT = ZHPQ_SERVER + '/depts/childrenRegionByDept?deptId='
const GET_DEPT_BY_REGION = ZHPQ_SERVER + '/depts/parentDeptByRegion?regionId='
const GET_DEPT_PATH = ZHPQ_SERVER + '/depts/'

export const getRegionByDept = deptId => {
  let path = GET_REGION_BY_DEPT + deptId
  return req.get(path)
}

export const getDeptByRegion = regionId => {
  let path = GET_DEPT_BY_REGION + regionId
  return req.get(path)
}

export const getDeptLevel = userId => {
  let path = DEPT_LEVEL_PATH + userId
  return req.get(path)
}

export const getGridLevel = data => {
  let path = GRID_LEVEL_PATH + data.userId + '&procsInsId=' + data.procsInsId + '&outcome=' + data.outcome
  return req.get(path)
}

export const createDept = data => {
  return req.post(DEPT_CREATE_PATH, data)
}

export const updateDept = data => {
  let path = buildPath(DEPT_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteDept = data => {
  let path = buildPath(DEPT_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const findDept = param => {
  return req.get(DEPT_FIND_PATH, param)
}

export const listDept = param => {
  return req.get(DEPT_LIST_PATH, param)
}

export const getDeptTree = param => {
  return req.get(DEPT_TREE_PATH, param)
}

export const getDept = id => {
  let path = GET_DEPT_PATH + id
  return req.get(path)
}

// 部门人员
const DEPT_MEMBER_FIND_PATH = ZHPQ_SERVER + '/deptMembers'
const DEPT_MEMBER_FIND_LIST_PATH = ZHPQ_SERVER + '/deptMembers/dept/'
const DEPT_MEMBER_DELETE_PATH = ZHPQ_SERVER + '/deptMembers/{ID}'

export const findDeptMember = param => {
  return req.get(DEPT_MEMBER_FIND_PATH, param)
}

export const findDeptMemberList = id => {
  let path = DEPT_MEMBER_FIND_LIST_PATH + id
  return req.get(path)
}

export const delDeptMember = data => {
  let path = buildPath(DEPT_MEMBER_DELETE_PATH, data.id)
  return req.delete(path)
}

// 网格
const GRID_CREATE_PATH = ZHPQ_SERVER + '/grids/'
const GRID_PUT_PATH = ZHPQ_SERVER + '/grids/{ID}'
const GRID_DELETE_PATH = ZHPQ_SERVER + '/grids/{ID}'
const GRID_FIND_PATH = ZHPQ_SERVER + '/grids'
const GRID_TREE_PATH = ZHPQ_SERVER + '/grids/tree'
const GRID_LIST_PATH = ZHPQ_SERVER + '/grids/list'
const GRID_ALL_PATH = ZHPQ_SERVER + '/grids/allTree'

// 网格
export const createGrid = data => {
  return req.post(GRID_CREATE_PATH, data)
}

export const updateGrid = data => {
  let path = buildPath(GRID_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteGrid = data => {
  let path = buildPath(GRID_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const findGrid = param => {
  return req.get(GRID_FIND_PATH, param)
}

export const getGridTree = param => {
  return req.get(GRID_TREE_PATH, param)
}

export const findGridTree = param => {
  return req.get(GRID_TREE_PATH, param)
}

export const listGrid = param => {
  return req.get(GRID_LIST_PATH, param)
}

export const getAllGridTree = param => {
  return req.get(GRID_ALL_PATH, param)
}

// 网格员
const GRID_MEMBER_CREATE_PATH = ZHPQ_SERVER + '/gridMember/'
const GRID_MEMBER_PUT_PATH = ZHPQ_SERVER + '/gridMember/{ID}'
const GRID_MEMBER_DELETE_PATH = ZHPQ_SERVER + '/gridMember/{ID}'
const GRID_MEMBER_FIND_PATH = ZHPQ_SERVER + '/gridMember'
const GRID_MEMBER_GET_PATH = ZHPQ_SERVER + '/gridMember/u'

// 网格
export const createGridMember = data => {
  return req.post(GRID_MEMBER_CREATE_PATH, data)
}

export const updateGridMember = data => {
  let path = buildPath(GRID_MEMBER_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteGridMember = data => {
  let path = buildPath(GRID_MEMBER_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const findGridMember = param => {
  return req.get(GRID_MEMBER_FIND_PATH, param)
}

export const getGridMember = param => {
  return req.get(GRID_MEMBER_GET_PATH, param)
}

// 获取key及对应value所有
// const DICT_KEY_LIST_PATH = ZHPQ_SERVER + '/gbDictionary/list'
const DICT_KEY_LIST_PATH = ZHPQ_SERVER + '/gbDictionary/value/list'

export const allDict = param => {
  return req.get(DICT_KEY_LIST_PATH, param)
}

// 字典key
const DICT_KEY_CREATE_PATH = ZHPQ_SERVER + '/gbDictionary/key'
const DICT_KEY_PUT_PATH = ZHPQ_SERVER + '/gbDictionary/key/{ID}'
const DICT_KEY_DELETE_PATH = ZHPQ_SERVER + '/gbDictionary/key/{ID}'
const DICT_KEY_FIND_PATH = ZHPQ_SERVER + '/gbDictionary/key'

// 字典key
export const createDictKey = data => {
  return req.post(DICT_KEY_CREATE_PATH, data)
}

export const updateDictKey = data => {
  let path = buildPath(DICT_KEY_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteDictKey = data => {
  let path = buildPath(DICT_KEY_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const findDictKey = param => {
  return req.get(DICT_KEY_FIND_PATH, param)
}

export const listDictKey = param => {
  return req.get(DICT_KEY_LIST_PATH, param)
}

// 字典value
const DICT_VALUE_CREATE_PATH = ZHPQ_SERVER + '/dictValues/'
const DICT_VALUE_PUT_PATH = ZHPQ_SERVER + '/dictValues/{ID}'
const DICT_VALUE_DELETE_PATH = ZHPQ_SERVER + '/dictValues/{ID}'
const DICT_VALUE_FIND_PATH = ZHPQ_SERVER + '/dictValues'
const DICT_VALUE_GET_PATH = ZHPQ_SERVER + '/dictValues/list'

export const getValueByKey = param => {
  let path = '/dictValues/list?keyName=' + param
  return req.get(path)
}

export const getAllDictValue = data => {
  return req.get(DICT_VALUE_GET_PATH, data)
}

export const createDictValue = data => {
  return req.post(DICT_VALUE_CREATE_PATH, data)
}

export const updateDictValue = data => {
  let path = buildPath(DICT_VALUE_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteDictValue = data => {
  let path = buildPath(DICT_VALUE_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const findDictValue = param => {
  return req.get(DICT_VALUE_FIND_PATH, param)
}

export const findDictValuesByKeyCode = keyCode => {
  let path = `/dictValues/${keyCode}/values`
  return req.get(path)
}

export const findSexList = _ => {
  return req.get('/dictValues/')
}

// 地图信息
const MAP_LOCATION_CREATE_PATH = ZHPQ_SERVER + '/mapLocation/'
const MAP_LOCATION_GET_PATH = ZHPQ_SERVER + '/mapLocation/{ID}'

export const createMapLocation = data => {
  let path = MAP_LOCATION_CREATE_PATH + '?t=' + Math.random()
  return req.post(path, data)
}

export const getMapLocation = data => {
  let path = buildPath(MAP_LOCATION_GET_PATH, data.id)
  return req.get(path)
}

export const updateMapLocation = data => {
  let path = '/mapLocation/' + data.id
  return req.put(path, data)
}

export const getMapLocationByTarget = data => {
  let path = `/mapLocation/${data.targetType}/up/${data.targetId}`
  return req.get(path)
}
export const getMapLocationDown = id => {
  let path = '/mapLocation/grid/down/' + id
  return req.get(path)
}

export const getMapLocationByTargetList = data => {
  let path = '/mapLocation/grid'
  return req.get(path)
}

export const getGridMapLocationByGridId = id => {
  let path = '/mapLocation/grid/' + id
  return req.get(path)
}

export const delMapLocationByTarget = target => {
  let path = '/mapLocation/' + target.targetType + '/' + target.targetId
  return req.delete(path)
}

// 记录用户操作日志
const LOG_LIST_PATH = ZHPQ_SERVER + '/actionLog'

export const logList = param => {
  return req.get(LOG_LIST_PATH, param)
}

// 国标字典
const DICTIONARY_FIND_PATH = ZHPQ_SERVER + '/gbDictionary/value'
const DICTIONARY_CREATE_PATH = ZHPQ_SERVER + '/gbDictionary/value'
const DICTIONARY_DELETE_PATH = ZHPQ_SERVER + '/gbDictionary/value/{ID}'
const DICTIONARY_PUT_PATH = ZHPQ_SERVER + '/gbDictionary/value/{ID}'
const DICTIONARY_KEY_LIST_PATH = ZHPQ_SERVER + '/gbDictionary/key/list'
const DICTIONARY_CREATETREENODE_PATH = ZHPQ_SERVER + '/gbDictionary/key'
const DICTIONARY_DELETETREENODE_PATH = ZHPQ_SERVER + '/gbDictionary/key/{ID}'
const DICTIONARY_UPDATETREENODE_PATH = ZHPQ_SERVER + '/gbDictionary/key/{ID}'
const DICTIONARY_LISTTREENODE_PATH = ZHPQ_SERVER + '/gbDictionary/key/{ID}'
const DICTIONARY_VALUE_LIST_HIERARCHY_PATH = ZHPQ_SERVER + '/gbDictionary/value/list'

export const findDictionaryValue = param => {
  return req.get(DICTIONARY_FIND_PATH, param)
}

export const creatDictionaryValue = data => {
  return req.post(DICTIONARY_CREATE_PATH, data)
}

export const delDictionaryValue = data => {
  let path = buildPath(DICTIONARY_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const updateDictionaryValue = data => {
  let path = buildPath(DICTIONARY_PUT_PATH, data.id)
  return req.put(path, data)
}

export const listDictionaryKey = param => {
  return req.get(DICTIONARY_KEY_LIST_PATH, param)
}

export const createTreeNode = data => {
  return req.post(DICTIONARY_CREATETREENODE_PATH, data)
}

export const delTreeNode = data => {
  let path = buildPath(DICTIONARY_DELETETREENODE_PATH, data.id)
  return req.delete(path, null)
}

export const updateTreeNode = data => {
  let path = buildPath(DICTIONARY_UPDATETREENODE_PATH, data.id)
  return req.put(path, data)
}

export const listTreeNode = data => {
  let path = buildPath(DICTIONARY_LISTTREENODE_PATH, data.id)
  return req.get(path, data)
}

export const listDictionaryValueHierarchy = param => {
  return req.get(DICTIONARY_VALUE_LIST_HIERARCHY_PATH, param)
}

// 组织
const ORGANIZATION_CREATE_PATH = ZHPQ_SERVER + '/organization/'
const ORGANIZATION_PUT_PATH = ZHPQ_SERVER + '/organization/{ID}'
const ORGANIZATION_DELETE_PATH = ZHPQ_SERVER + '/organization/{ID}'
const ORGANIZATION_FIND_PATH = ZHPQ_SERVER + '/organization'
const ORGANIZATION_LIST_FIND_PATH = ZHPQ_SERVER + '/organization/list'

export const createOrganization = data => {
  return req.post(ORGANIZATION_CREATE_PATH, data)
}

export const updateOrganization = data => {
  let path = buildPath(ORGANIZATION_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteOrganization = data => {
  let path = buildPath(ORGANIZATION_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const findOrganization = param => {
  return req.get(ORGANIZATION_FIND_PATH, param)
}

export const getOrganizationList = deptId => {
  let path = deptId ? ORGANIZATION_LIST_FIND_PATH + '?deptId=' + deptId : ORGANIZATION_LIST_FIND_PATH
  return req.get(path)
}
// 队伍
const ORGANIZATIONMEMBER_CREATE_PATH = ZHPQ_SERVER + '/organizationMember/'
const ORGANIZATIONMEMBER_PUT_PATH = ZHPQ_SERVER + '/organizationMember/{ID}'
const ORGANIZATIONMEMBER_DELETE_PATH = ZHPQ_SERVER + '/organizationMember/{ID}'
const ORGANIZATIONMEMBER_FIND_PATH = ZHPQ_SERVER + '/organizationMember'
const ORGANIZATIONMEMBER_GET_PATH = ZHPQ_SERVER + '/organizationMember/'

export const createOrganizationMember = data => {
  return req.post(ORGANIZATIONMEMBER_CREATE_PATH, data)
}

export const updateOrganizationMember = data => {
  let path = buildPath(ORGANIZATIONMEMBER_PUT_PATH, data.id)
  return req.put(path, data)
}

export const deleteOrganizationMember = data => {
  let path = buildPath(ORGANIZATIONMEMBER_DELETE_PATH, data.id)
  return req.delete(path, null)
}

export const findOrganizationMember = param => {
  return req.get(ORGANIZATIONMEMBER_FIND_PATH, param)
}

export const getOrganizationMember = id => {
  let path = ORGANIZATIONMEMBER_GET_PATH + id
  return req.get(path)
}

export const getUserByorgId = orgId => {
  let path = '/organizationMember/list?orgId=' + orgId
  return req.get(path)
}

// 天气接口
export const getWeather = _ => {
  let data = {
    version: 'v1',
    cityid: '101180601'
  }
  let path = 'http://www.tianqiapi.com/api/'
  return req.get(path, data)
}

// 综治实战大数据
// 实有人口
const MARITAL_STATUS = ZHPQ_SERVER + '/syrk/hjrk'
const CENSUS_REGISTER_AGE = ZHPQ_SERVER + '/syrk/hjrk/nlgc'
const EDUCATION_CONSTITUTE = ZHPQ_SERVER + '/syrk/hjrk/xlcx'
const REASONS_OF_FLOW = ZHPQ_SERVER + '/syrk/ldrk'
const FLOATING_POPULATION = ZHPQ_SERVER + '/syrk/ldrk/nlgc'
const OVERSEAS_PEOPLE = ZHPQ_SERVER + '/syrk/overseas'
const RENTAL_ANALYSIS = ZHPQ_SERVER + '/syrk/rentalHouse'
const FLOATING_POPULATION_EDUCATION = ZHPQ_SERVER + '/syrk/ldrk/xlcx'

export const maritalStatus = param => {
  return req.get(MARITAL_STATUS, param)
}
export const censusRegisterAge = param => {
  return req.get(CENSUS_REGISTER_AGE, param)
}
export const educationConstitute = param => {
  return req.get(EDUCATION_CONSTITUTE, param)
}
export const reasonsOfFlow = param => {
  return req.get(REASONS_OF_FLOW, param)
}
export const floatingPopulation = param => {
  return req.get(FLOATING_POPULATION, param)
}
export const overseasPeople = param => {
  return req.get(OVERSEAS_PEOPLE, param)
}
export const rentalAnalysis = param => {
  return req.get(RENTAL_ANALYSIS, param)
}
export const floatingPopulationEducation = param => {
  return req.get(FLOATING_POPULATION_EDUCATION, param)
}
// 特殊人群
const COMMUNITY_CORRECTION = ZHPQ_SERVER + '/specialPopulation/corrector'
const EDUCATION_CORRECTION = ZHPQ_SERVER + '/specialPopulation/corrector/education'

export const communityCorrection = param => {
  return req.get(COMMUNITY_CORRECTION, param)
}
export const educationCorrection = param => {
  return req.get(EDUCATION_CORRECTION, param)
}

// 导出
export const getExportFileUrl = _ => {
  let path = ZHPQ_SERVER + '/SignInfo/export'
  return path
}

// 通知发送
export const createNotice = data => {
  let path = '/notice/'
  return req.post(path, data)
}
export const findNotice = _ => {
  let path = '/notice'
  return req.get(path)
}
export const getNotice = id => {
  let path = '/notice/' + id
  return req.get(path)
}
export const updataNotice = data => {
  let path = '/notice/' + data.id
  return req.put(path, data)
}
export const deleteNotice = data => {
  let path = '/notice/' + data.id
  return req.delete(path, data)
}
